################################################################################################
###Replication Code for Online Appendix for "`Because He's Gay': How Race, Gender, and Sexuality Shape Perceptions of Judicial Fairness"
###Journal of Politics
###Authors: Ana Bracic, Mackenzie Israel-Trummel, Tyler Johnson, and Kathleen Tipler
################################



################################
##Instructions:
##Run Code files 1_Conjoint Set Up.R, 2_Conjoint Analysis Democrats.R, and 3_Conjoint Analysis_Republicans.R before running this file. 
################################


##########################################################################
## Models for Case Type Figures A1 to A5
##########################################################################



##############################
##Religious Discrimination
##############################
modD.relig <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                + factor(vignette_id),
                data = subset(dems, discrimtype == "religious"))

# use new summary function
summary(modD.relig)
modD.relig2 <- summary(modD.relig, cluster = c("id"))


coefs.relig.D <- c(modD.relig2$coef[,1]) #Pull coefficients from model
confint.relig.D <- c(modD.relig2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmprelig.D <- data.frame(cbind(coefs.relig.D, confint.relig.D))



##Create slimmed data frame to just attributes for plot

tmprelig.D2 <- tmprelig.D[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmprelig.D)),]
tmprelig.D2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmprelig.D2$variable <- as.factor(tmprelig.D2$variable)
names(tmprelig.D2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesrelig.D <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesrelig.D)[1]){
  if (sum(tmprelig.D2$variable == variablesrelig.D$variable[i], na.rm = T) > 0){
    variablesrelig.D$coef[i] <- tmprelig.D2$coef[tmprelig.D2$variable == variablesrelig.D$variable[i]]
    variablesrelig.D$confint[i] <- tmprelig.D2$confint[tmprelig.D2$variable == variablesrelig.D$variable[i]]
  }
}


## Reorder for plotting
variablesrelig2.D <- variablesrelig.D

order <- 1:dim(variablesrelig2.D)[1]
variablesrelig2.D <- transform(variablesrelig2.D, variable2 = reorder(variable2, -order) ) 

variablesrelig2.D$labels <- round(variablesrelig2.D$coef, 3)
variablesrelig2.D$labels[variablesrelig2.D$labels == 0] <- NA



plotrelig.D <- ggplot(data = variablesrelig2.D, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh( aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group),size=.5)+
  scale_color_manual(values=c("#376795", "#376795", "#376795", "#376795")) + 
  scale_shape_manual(values=c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "ReligCase_Dems.pdf", height = 9, width = 8)
plotrelig.D
dev.off()


## Republican respondents

modR.relig <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(reps, discrimtype == "religious"))

# use new summary function
summary(modR.relig)
modR.relig2 <- summary(modR.relig, cluster = c("id"))


coefs.relig.R <- c(modR.relig2$coef[,1]) #Pull coefficients from model
confint.relig.R <- c(modR.relig2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmprelig.R <- data.frame(cbind(coefs.relig.R, confint.relig.R))



##Create slimmed data frame to just attributes for plot

tmprelig.R2 <- tmprelig.R[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmprelig.R)),]
tmprelig.R2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmprelig.R2$variable <- as.factor(tmprelig.R2$variable)
names(tmprelig.R2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesrelig.R <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesrelig.R)[1]){
  if (sum(tmprelig.R2$variable == variablesrelig.R$variable[i], na.rm = T) > 0){
    variablesrelig.R$coef[i] <- tmprelig.R2$coef[tmprelig.R2$variable == variablesrelig.R$variable[i]]
    variablesrelig.R$confint[i] <- tmprelig.R2$confint[tmprelig.R2$variable == variablesrelig.R$variable[i]]
  }
}


## Reorder for plotting
variablesrelig2.R <- variablesrelig.R

order <- 1:dim(variablesrelig2.R)[1]
variablesrelig2.R <- transform(variablesrelig2.R, variable2 = reorder(variable2, -order) ) 

variablesrelig2.R$labels <- round(variablesrelig2.R$coef, 3)
variablesrelig2.R$labels[variablesrelig2.R$labels == 0] <- NA



plotrelig.R <- ggplot(data = variablesrelig2.R, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group), size=.5)+
  scale_color_manual(values = c("#e76254", "#e76254", "#e76254", "#e76254")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "ReligCase_Reps.pdf", height = 9, width = 8)
plotrelig.R
dev.off()



##############################
## Sex Discrimination
##############################
modD.sex <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(dems, discrimtype == "gender"))

# use new summary function
summary(modD.sex)
modD.sex2 <- summary(modD.sex, cluster = c("id"))


coefs.sex.D <- c(modD.sex2$coef[,1]) #Pull coefficients from model
confint.sex.D <- c(modD.sex2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmpsex.D <- data.frame(cbind(coefs.sex.D, confint.sex.D))



##Create slimmed data frame to just attributes for plot

tmpsex.D2 <- tmpsex.D[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmpsex.D)),]
tmpsex.D2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmpsex.D2$variable <- as.factor(tmpsex.D2$variable)
names(tmpsex.D2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablessex.D <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablessex.D)[1]){
  if (sum(tmpsex.D2$variable == variablessex.D$variable[i], na.rm = T) > 0){
    variablessex.D$coef[i] <- tmpsex.D2$coef[tmpsex.D2$variable == variablessex.D$variable[i]]
    variablessex.D$confint[i] <- tmpsex.D2$confint[tmpsex.D2$variable == variablessex.D$variable[i]]
  }
}


## Reorder for plotting
variablessex2.D <- variablessex.D

order <- 1:dim(variablessex2.D)[1]
variablessex2.D <- transform(variablessex2.D, variable2 = reorder(variable2, -order) ) 

variablessex2.D$labels <- round(variablessex2.D$coef, 3)
variablessex2.D$labels[variablessex2.D$labels == 0] <- NA



plotsex.D <- ggplot(data = variablessex2.D, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height =.28, colour = group), size = .5)+
  scale_color_manual(values = c("#376795", "#376795", "#376795", "#376795")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "SexCase_Dems.pdf", height = 9, width = 8)
plotsex.D
dev.off()


## Republican respondents

modR.sex <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(reps, discrimtype == "gender"))

# use new summary function
summary(modR.sex)
modR.sex2 <- summary(modR.sex, cluster = c("id"))


coefs.sex.R <- c(modR.sex2$coef[,1]) #Pull coefficients from model
confint.sex.R <- c(modR.sex2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmpsex.R <- data.frame(cbind(coefs.sex.R, confint.sex.R))



##Create slimmed data frame to just attributes for plot

tmpsex.R2 <- tmpsex.R[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican", row.names(tmpsex.R)),]
tmpsex.R2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmpsex.R2$variable <- as.factor(tmpsex.R2$variable)
names(tmpsex.R2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablessex.R <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablessex.R)[1]){
  if (sum(tmpsex.R2$variable == variablessex.R$variable[i], na.rm = T) > 0){
    variablessex.R$coef[i] <- tmpsex.R2$coef[tmpsex.R2$variable == variablessex.R$variable[i]]
    variablessex.R$confint[i] <- tmpsex.R2$confint[tmpsex.R2$variable == variablessex.R$variable[i]]
  }
}


## Reorder for plotting
variablessex2.R <- variablessex.R

order <- 1:dim(variablessex2.R)[1]
variablessex2.R <- transform(variablessex2.R, variable2 = reorder(variable2, -order) ) 

variablessex2.R$labels <- round(variablessex2.R$coef, 3)
variablessex2.R$labels[variablessex2.R$labels == 0] <- NA



plotsex.R <- ggplot(data = variablessex2.R, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group), size=.5)+
  scale_color_manual(values = c("#e76254", "#e76254", "#e76254", "#e76254")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "SexCase_Reps.pdf", height = 9, width = 8)
plotsex.R
dev.off()






##############################
## Transgender ID Discrimination
##############################
modD.trans <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
               + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
               + factor(vignette_id),
               data = subset(dems, discrimtype == "gender identity"))

# use new summary function
summary(modD.trans)
modD.trans2 <- summary(modD.trans, cluster = c("id"))


coefs.trans.D <- c(modD.trans2$coef[,1]) #Pull coefficients from model
confint.trans.D <- c(modD.trans2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmptrans.D <- data.frame(cbind(coefs.trans.D, confint.trans.D))



##Create slimmed data frame to just attributes for plot

tmptrans.D2 <- tmptrans.D[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmptrans.D)),]
tmptrans.D2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmptrans.D2$variable <- as.factor(tmptrans.D2$variable)
names(tmptrans.D2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablestrans.D <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablestrans.D)[1]){
  if (sum(tmptrans.D2$variable == variablestrans.D$variable[i], na.rm = T) > 0){
    variablestrans.D$coef[i] <- tmptrans.D2$coef[tmptrans.D2$variable == variablestrans.D$variable[i]]
    variablestrans.D$confint[i] <- tmptrans.D2$confint[tmptrans.D2$variable == variablestrans.D$variable[i]]
  }
}


## Reorder for plotting
variablestrans2.D <- variablestrans.D

order <- 1:dim(variablestrans2.D)[1]
variablestrans2.D <- transform(variablestrans2.D, variable2 = reorder(variable2, -order) ) 

variablestrans2.D$labels <- round(variablestrans2.D$coef, 3)
variablestrans2.D$labels[variablestrans2.D$labels == 0] <- NA



plottrans.D <- ggplot(data = variablestrans2.D, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height =.28, colour = group), size = .5)+
  scale_color_manual(values = c("#376795", "#376795", "#376795", "#376795")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "TransCase_Dems.pdf", height = 9, width = 8)
plottrans.D
dev.off()


## Republican respondents

modR.trans <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
               + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
               + factor(vignette_id),
               data = subset(reps, discrimtype == "gender identity"))

# use new summary function
summary(modR.trans)
modR.trans2 <- summary(modR.trans, cluster = c("id"))


coefs.trans.R <- c(modR.trans2$coef[,1]) #Pull coefficients from model
confint.trans.R <- c(modR.trans2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmptrans.R <- data.frame(cbind(coefs.trans.R, confint.trans.R))



##Create slimmed data frame to just attributes for plot

tmptrans.R2 <- tmptrans.R[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican", row.names(tmptrans.R)),]
tmptrans.R2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmptrans.R2$variable <- as.factor(tmptrans.R2$variable)
names(tmptrans.R2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablestrans.R <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablestrans.R)[1]){
  if (sum(tmptrans.R2$variable == variablestrans.R$variable[i], na.rm = T) > 0){
    variablestrans.R$coef[i] <- tmptrans.R2$coef[tmptrans.R2$variable == variablestrans.R$variable[i]]
    variablestrans.R$confint[i] <- tmptrans.R2$confint[tmptrans.R2$variable == variablestrans.R$variable[i]]
  }
}


## Reorder for plotting
variablestrans2.R <- variablestrans.R

order <- 1:dim(variablestrans2.R)[1]
variablestrans2.R <- transform(variablestrans2.R, variable2 = reorder(variable2, -order) ) 

variablestrans2.R$labels <- formatC(variablestrans2.R$coef, format = "f", digits = 3) #to make sure last 0 shows up for one of the labels
variablestrans2.R$labels[variablestrans2.R$labels == "0.000"] <- NA 



plottrans.R <- ggplot(data = variablestrans2.R, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group), size=.5)+
  scale_color_manual(values = c("#e76254", "#e76254", "#e76254", "#e76254")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "TransCase_Reps.pdf", height = 9, width = 8)
plottrans.R
dev.off()






##############################
## Racial Discrimination
##############################
modD.race <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(dems, discrimtype == "racial"))

# use new summary function
summary(modD.race)
modD.race2 <- summary(modD.race, cluster = c("id"))


coefs.race.D <- c(modD.race2$coef[,1]) #Pull coefficients from model
confint.race.D <- c(modD.race2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmprace.D <- data.frame(cbind(coefs.race.D, confint.race.D))



##Create slimmed data frame to just attributes for plot

tmprace.D2 <- tmprace.D[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmprace.D)),]
tmprace.D2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmprace.D2$variable <- as.factor(tmprace.D2$variable)
names(tmprace.D2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesrace.D <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesrace.D)[1]){
  if (sum(tmprace.D2$variable == variablesrace.D$variable[i], na.rm = T) > 0){
    variablesrace.D$coef[i] <- tmprace.D2$coef[tmprace.D2$variable == variablesrace.D$variable[i]]
    variablesrace.D$confint[i] <- tmprace.D2$confint[tmprace.D2$variable == variablesrace.D$variable[i]]
  }
}


## Reorder for plotting
variablesrace2.D <- variablesrace.D

order <- 1:dim(variablesrace2.D)[1]
variablesrace2.D <- transform(variablesrace2.D, variable2 = reorder(variable2, -order) ) 

variablesrace2.D$labels <- round(variablesrace2.D$coef, 3)
variablesrace2.D$labels[variablesrace2.D$labels == 0] <- NA



plotrace.D <- ggplot(data = variablesrace2.D, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height =.28, colour = group), size = .5)+
  scale_color_manual(values = c("#376795", "#376795", "#376795", "#376795")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "RaceCase_Dems.pdf", height = 9, width = 8)
plotrace.D
dev.off()




## Republican respondents

modR.race <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(reps, discrimtype == "racial"))

# use new summary function
summary(modR.race)
modR.race2 <- summary(modR.race, cluster = c("id"))


coefs.race.R <- c(modR.race2$coef[,1]) #Pull coefficients from model
confint.race.R <- c(modR.race2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmprace.R <- data.frame(cbind(coefs.race.R, confint.race.R))



##Create slimmed data frame to just attributes for plot

tmprace.R2 <- tmprace.R[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican", row.names(tmprace.R)),]
tmprace.R2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmprace.R2$variable <- as.factor(tmprace.R2$variable)
names(tmprace.R2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesrace.R <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesrace.R)[1]){
  if (sum(tmprace.R2$variable == variablesrace.R$variable[i], na.rm = T) > 0){
    variablesrace.R$coef[i] <- tmprace.R2$coef[tmprace.R2$variable == variablesrace.R$variable[i]]
    variablesrace.R$confint[i] <- tmprace.R2$confint[tmprace.R2$variable == variablesrace.R$variable[i]]
  }
}


## Reorder for plotting
variablesrace2.R <- variablesrace.R

order <- 1:dim(variablesrace2.R)[1]
variablesrace2.R <- transform(variablesrace2.R, variable2 = reorder(variable2, -order) ) 

variablesrace2.R$labels <- formatC(variablesrace2.R$coef, format = "f", digits = 3) #to make sure last 0 shows up for one of the labels
variablesrace2.R$labels[variablesrace2.R$labels == "0.000"] <- NA 



plotrace.R <- ggplot(data = variablesrace2.R, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group), size=.5)+
  scale_color_manual(values = c("#e76254", "#e76254", "#e76254", "#e76254")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "RaceCase_Reps.pdf", height = 9, width = 8)
plotrace.R
dev.off()






##############################
## Sexuality Discrimination
##############################
modD.orien <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(dems, discrimtype == "sexual orientation"))

# use new summary function
summary(modD.orien)
modD.orien2 <- summary(modD.orien, cluster = c("id"))


coefs.orien.D <- c(modD.orien2$coef[,1]) #Pull coefficients from model
confint.orien.D <- c(modD.orien2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmporien.D <- data.frame(cbind(coefs.orien.D, confint.orien.D))



##Create slimmed data frame to just attributes for plot

tmporien.D2 <- tmporien.D[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican",row.names(tmporien.D)),]
tmporien.D2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmporien.D2$variable <- as.factor(tmporien.D2$variable)
names(tmporien.D2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesorien.D <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesorien.D)[1]){
  if (sum(tmporien.D2$variable == variablesorien.D$variable[i], na.rm = T) > 0){
    variablesorien.D$coef[i] <- tmporien.D2$coef[tmporien.D2$variable == variablesorien.D$variable[i]]
    variablesorien.D$confint[i] <- tmporien.D2$confint[tmporien.D2$variable == variablesorien.D$variable[i]]
  }
}


## Reorder for plotting
variablesorien2.D <- variablesorien.D

order <- 1:dim(variablesorien2.D)[1]
variablesorien2.D <- transform(variablesorien2.D, variable2 = reorder(variable2, -order) ) 

variablesorien2.D$labels <- round(variablesorien2.D$coef, 3)
variablesorien2.D$labels[variablesorien2.D$labels == 0] <- NA



plotorien.D <- ggplot(data = variablesorien2.D, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height =.28, colour = group), size = .5)+
  scale_color_manual(values = c("#376795", "#376795", "#376795", "#376795")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "SexOrienCase_Dems.pdf", height = 9, width = 8)
plotorien.D
dev.off()


## Republican respondents

modR.orien <- lm(selected ~ race + gender + sexuality + party + agecat + lawschool + occupation
                 + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                 + factor(vignette_id),
                 data = subset(reps, discrimtype == "sexual orientation"))

# use new summary function
summary(modR.orien)
modR.orien2 <- summary(modR.orien, cluster = c("id"))


coefs.orien.R <- c(modR.orien2$coef[,1]) #Pull coefficients from model
confint.orien.R <- c(modR.orien2$coef[,2]*1.96) #Create 95 percent confidence interval from standard error 

## Join into data frame
tmporien.R <- data.frame(cbind(coefs.orien.R, confint.orien.R))



##Create slimmed data frame to just attributes for plot

tmporien.R2 <- tmporien.R[grep("^race|^genderWoman$|^sexualityGay$|^partyRepublican", row.names(tmporien.R)),]
tmporien.R2$variable <- c("Asian", "Black", "Hispanic", "Native", "Woman", "Gay", "Republican")
tmporien.R2$variable <- as.factor(tmporien.R2$variable)
names(tmporien.R2) <- c("coef", "confint", "variable")



## Load list of variables for plot axis
variablesorien.R <- read.csv(file = "conjointvariablesSHORT.csv")


## Input data for plotting
for (i in 1:dim(variablesorien.R)[1]){
  if (sum(tmporien.R2$variable == variablesorien.R$variable[i], na.rm = T) > 0){
    variablesorien.R$coef[i] <- tmporien.R2$coef[tmporien.R2$variable == variablesorien.R$variable[i]]
    variablesorien.R$confint[i] <- tmporien.R2$confint[tmporien.R2$variable == variablesorien.R$variable[i]]
  }
}


## Reorder for plotting
variablesorien2.R <- variablesorien.R

order <- 1:dim(variablesorien2.R)[1]
variablesorien2.R <- transform(variablesorien2.R, variable2 = reorder(variable2, -order) ) 

variablesorien2.R$labels <- formatC(variablesorien2.R$coef, format = "f", digits = 3) #to make sure last 0 shows up for one of the labels
variablesorien2.R$labels[variablesorien2.R$labels == "0.000"] <- NA 



plotorien.R <- ggplot(data = variablesorien2.R, aes(x = coef, y = variable2)) +
  geom_point(aes(colour = group, shape = group), size = 4.5)+
  geom_text(aes(label = labels), nudge_y = 0.4, nudge_x = -0.002, size=6)+
  geom_errorbarh(aes(y = variable2, xmin = coef - confint, xmax = coef + confint, height=.28,colour=group), size=.5)+
  scale_color_manual(values = c("#e76254", "#e76254", "#e76254", "#e76254")) + 
  scale_shape_manual(values = c(16, 16, 16,  16)) + 
  theme_bw() + 
  xlim(-0.2, 0.2) + 
  xlab("Change in Judge Preference")+
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_text(size = 20)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black", size = 20))  +
  theme(axis.text.x = element_text(size = 20))  +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1, .5, 0.1, .5), "cm"))


pdf(file = "SexOrienCase_Reps.pdf", height = 9, width = 8)
plotorien.R
dev.off()




#########################################################################
## Regression Models for Appendix Table A1: Democrats
#########################################################################

library(texreg)

##NOTE: STANDARD ERRORS NEED TO BE MANUALLY INPUTTED AND SIGNIFICANCE ADJUSTED. THESE ARE FROM NON-ROBUST CLUSTERED MODEL

texreg(list(modC.D, modS.D,  modP.D, modT.D),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "Republican",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "Latino",
         "Multiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:demregs", stars = c(0.05), caption = "Democratic Respondents", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors
err.dem.conserv <- as.data.frame(modC2.D$coefficients[,2])
names(err.dem.conserv) <- c("Error")
err.dem.conserv$err2 <- round(err.dem.conserv$Error, 2)
err.dem.conserv

err.dem.select <- as.data.frame(modS2.D$coefficients[,2])
names(err.dem.select) <- c("Error")
err.dem.select$err2 <- round(err.dem.select$Error, 2)
err.dem.select

err.dem.polmotive <- as.data.frame(modP2.D$coefficients[,2])
names(err.dem.polmotive) <- c("Error")
err.dem.polmotive$err2 <- round(err.dem.polmotive$Error, 2)
err.dem.polmotive

err.dem.trustussc <- as.data.frame(modT2.D$coefficients[,2])
names(err.dem.trustussc) <- c("Error")
err.dem.trustussc$err2 <- round(err.dem.trustussc$Error, 2)
err.dem.trustussc




######################################################################
## Regression Models for Appendix Table 2: Republicans
######################################################################

texreg(list(modC.R, modS.R,  modP.R, modT.R),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "Republican",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "Latino",
         "Multiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:repregs", stars = c(0.05), caption = "Republican Respondents", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors
err.rep.conserv <- as.data.frame(modC2.R$coefficients[,2])
names(err.rep.conserv) <- c("Error")
err.rep.conserv$err2 <- round(err.rep.conserv$Error, 2)
err.rep.conserv

err.rep.select <- as.data.frame(modS2.R$coefficients[,2])
names(err.rep.select) <- c("Error")
err.rep.select$err2 <- round(err.rep.select$Error, 2)
err.rep.select

err.rep.polmotive <- as.data.frame(modP2.R$coefficients[,2])
names(err.rep.polmotive) <- c("Error")
err.rep.polmotive$err2 <- round(err.rep.polmotive$Error, 2)
err.rep.polmotive

err.rep.trustussc <- as.data.frame(modT2.R$coefficients[,2])
names(err.rep.trustussc) <- c("Error")
err.rep.trustussc$err2 <- round(err.rep.trustussc$Error, 2)
err.rep.trustussc


######################################################################
## Regression Models for Appendix Tables 3-6: Subsets by Respondent and Judge Partisanship
######################################################################


## Subset dems data by judge partisanship

table(dems$party)

dems.dem <- subset(dems, party == "Democrat")
dems.rep <- subset(dems, party == "Republican")

######### Table 3: Democratic Respondents and Democratic Judges

## Judge ideology DV

dems.dem.ideo <- lm(conservative_1 ~ race + gender + sexuality + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    + factor(vignette_id),
                    data = dems.dem)

# use new summary function
summary(dems.dem.ideo)
dems.dem.ideo2 <- summary(dems.dem.ideo, cluster = c("id"))
dems.dem.ideo2


## Select judge DV

dems.dem.sel <- lm(selected ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   +factor(vignette_id),
                   data = dems.dem)

# use new summary function
summary(dems.dem.sel)
dems.dem.sel2 <- summary(dems.dem.sel,cluster = c("id"))
dems.dem.sel2



##political motivation DV

dems.dem.pol <- lm(political_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   +factor(vignette_id),
                   data = dems.dem)

# use new summary function
summary(dems.dem.pol)
dems.dem.pol2 <- summary(dems.dem.pol, cluster = c("id"))
dems.dem.pol2



## USSC trust DV

dems.dem.ussc <- lm(trustcourt_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    +factor(vignette_id),
                    data = dems.dem)

# use new summary function
summary(dems.dem.ussc)
dems.dem.ussc2 <- summary(dems.dem.ussc, cluster = c("id"))
dems.dem.ussc2




texreg(list(dems.dem.ideo, dems.dem.sel,  dems.dem.pol, dems.dem.ussc),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "RLatino",
         "RMultiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:DemRsDemJs", stars = c(0.05), caption = "Conjoint Experiment Regression Models, Democratic Respondents and Democratic Judges", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors and update significance of effects
err.demsdem.conserv <- as.data.frame(dems.dem.ideo2$coefficients[, 2])
names(err.demsdem.conserv) <- c("Error")
err.demsdem.conserv$err2 <- round(err.demsdem.conserv$Error, 2)
err.demsdem.conserv

err.demsdem.sel <- as.data.frame(dems.dem.sel2$coefficients[, 2])
names(err.demsdem.sel) <- c("Error")
err.demsdem.sel$err2 <- round(err.demsdem.sel$Error, 2)
err.demsdem.sel

err.demsdem.pol <- as.data.frame(dems.dem.pol2$coefficients[, 2])
names(err.demsdem.pol) <- c("Error")
err.demsdem.pol$err2 <- round(err.demsdem.pol$Error, 2)
err.demsdem.pol

err.demsdem.ussc <- as.data.frame(dems.dem.ussc2$coefficients[, 2])
names(err.demsdem.ussc) <- c("Error")
err.demsdem.ussc$err2 <- round(err.demsdem.ussc$Error, 2)
err.demsdem.ussc




######### Table 4: Democratic Respondents and Republican Judges


## Judge ideology DV
dems.rep.ideo <- lm(conservative_1 ~ race + gender + sexuality + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    +factor(vignette_id),
                    data = dems.rep)

# use new summary function
summary(dems.rep.ideo)
dems.rep.ideo2 <- summary(dems.rep.ideo, cluster = c("id"))
dems.rep.ideo2


## Select judge DV

dems.rep.sel <- lm(selected ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   +factor(vignette_id),
                   data = dems.rep)

# use new summary function
summary(dems.rep.sel)
dems.rep.sel2 <- summary(dems.rep.sel, cluster = c("id"))
dems.rep.sel2


## Political motivation DV

dems.rep.pol <- lm(political_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   +factor(vignette_id),
                   data = dems.rep)

# use new summary function
summary(dems.rep.pol)
dems.rep.pol2 <- summary(dems.rep.pol, cluster = c("id"))
dems.rep.pol2


## USSC trust DV


dems.rep.ussc <- lm(trustcourt_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    +factor(vignette_id),
                    data = dems.rep)

# use new summary function
summary(dems.rep.ussc)
dems.rep.ussc2 <- summary(dems.rep.ussc, cluster = c("id"))
dems.rep.ussc2




texreg(list(dems.rep.ideo, dems.rep.sel,  dems.rep.pol, dems.rep.ussc),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "RLatino",
         "RMultiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:DemRsRepJs", stars = c(0.05), caption = "Conjoint Experiment Regression Models, Democratic Respondents and Republican Judges", float.pos = "hb", single.row = TRUE)


##get robust clustered standard errors and update significance of effects
err.demsrep.conserv <- as.data.frame(dems.rep.ideo2$coefficients[, 2])
names(err.demsrep.conserv) <- c("Error")
err.demsrep.conserv$err2 <- round(err.demsrep.conserv$Error, 2)
err.demsrep.conserv

err.demsrep.sel <- as.data.frame(dems.rep.sel2$coefficients[, 2])
names(err.demsrep.sel) <- c("Error")
err.demsrep.sel$err2 <- round(err.demsrep.sel$Error, 2)
err.demsrep.sel

err.demsrep.pol <- as.data.frame(dems.rep.pol2$coefficients[, 2])
names(err.demsrep.pol) <- c("Error")
err.demsrep.pol$err2 <- round(err.demsrep.pol$Error, 2)
err.demsrep.pol

err.demsrep.ussc <- as.data.frame(dems.rep.ussc2$coefficients[, 2])
names(err.demsrep.ussc) <- c("Error")
err.demsrep.ussc$err2 <- round(err.demsrep.ussc$Error, 2)
err.demsrep.ussc


############# Republican Respondents

## Subset reps data by judge partisanship


table(reps$party)

reps.dem <- subset(reps, party == "Democrat")
reps.rep <- subset(reps, party == "Republican")

######### Table 5: Republican Respondents and Democratic Judges

## Judge ideology DV
reps.dem.ideo <- lm(conservative_1 ~ race + gender + sexuality + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    + factor(vignette_id),
                    data = reps.dem)

# use new summary function
summary(reps.dem.ideo)
reps.dem.ideo2 <- summary(reps.dem.ideo, cluster = c("id"))
reps.dem.ideo2



## Select judge DV

reps.dem.sel <- lm(selected ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   + factor(vignette_id),
                   data = reps.dem)

# use new summary function
summary(reps.dem.sel)
reps.dem.sel2 <- summary(reps.dem.sel, cluster = c("id"))
reps.dem.sel2


## Political motivation DV

reps.dem.pol <- lm(political_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   +factor(vignette_id),
                   data = reps.dem)

# use new summary function
summary(reps.dem.pol)
reps.dem.pol2 <- summary(reps.dem.pol, cluster = c("id"))
reps.dem.pol2




## USSC trust DV

reps.dem.ussc <- lm(trustcourt_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    +factor(vignette_id),
                    data = reps.dem)

# use new summary function
summary(reps.dem.ussc)
reps.dem.ussc2 <- summary(reps.dem.ussc, cluster = c("id"))
reps.dem.ussc2


##Republicans Evaluating Democratic Judges

texreg(list(reps.dem.ideo, reps.dem.sel,  reps.dem.pol, reps.dem.ussc),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "RLatino",
         "RMultiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:RepRsDemJs", stars = c(0.05), caption = "Conjoint Experiment Regression Models, Republican Respondents and Democratic Judges", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors and update significance of effects
err.repsdem.conserv <- as.data.frame(reps.dem.ideo2$coefficients[, 2])
names(err.repsdem.conserv) <- c("Error")
err.repsdem.conserv$err2 <- round(err.repsdem.conserv$Error, 2)
err.repsdem.conserv

err.repsdem.sel <- as.data.frame(reps.dem.sel2$coefficients[, 2])
names(err.repsdem.sel) <- c("Error")
err.repsdem.sel$err2 <- round(err.repsdem.sel$Error, 2)
err.repsdem.sel

err.repsdem.pol <- as.data.frame(reps.dem.pol2$coefficients[, 2])
names(err.repsdem.pol) <- c("Error")
err.repsdem.pol$err2 <- round(err.repsdem.pol$Error, 2)
err.repsdem.pol

err.repsdem.ussc <- as.data.frame(reps.dem.ussc2$coefficients[, 2])
names(err.repsdem.ussc) <- c("Error")
err.repsdem.ussc$err2 <- round(err.repsdem.ussc$Error, 2)
err.repsdem.ussc



######### Table 6: Republican Respondents and Republican Judges

## Judge ideology DV

reps.rep.ideo <- lm(conservative_1 ~ race + gender + sexuality + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    + factor(vignette_id),
                    data = reps.rep)

# use new summary function
summary(reps.rep.ideo)
reps.rep.ideo2 <- summary(reps.rep.ideo, cluster = c("id"))
reps.rep.ideo2


## Select judge DV

reps.rep.sel <- lm(selected ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   + factor(vignette_id),
                   data = reps.rep)

# use new summary function
summary(reps.rep.sel)
reps.rep.sel2 <- summary(reps.rep.sel, cluster = c("id"))
reps.rep.sel2


## Political motivation DV

reps.rep.pol <- lm(political_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                   + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                   + factor(vignette_id),
                   data = reps.rep)

# use new summary function
summary(reps.rep.pol)
reps.rep.pol2 <- summary(reps.rep.pol, cluster = c("id"))
reps.rep.pol2



## USSC trust DV

reps.rep.ussc <- lm(trustcourt_1 ~ race + gender + sexuality  + agecat + lawschool + occupation
                    + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                    + factor(vignette_id),
                    data = reps.rep)

# use new summary function
summary(reps.rep.ussc)
reps.rep.ussc2 <- summary(reps.rep.ussc, cluster = c("id"))
reps.rep.ussc2



texreg(list(reps.rep.ideo, reps.rep.sel,  reps.rep.pol, reps.rep.ussc),
       custom.coef.names = c( 
         "(Intercept)",
         "Asian",
         "Black",
         "Hispanic",
         "Native",
         "Woman",
         "Gay",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "RLatino",
         "RMultiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Conservatism", "Preferred Judge", "Politically Motivated", "USSC Trust"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:RepRsRepJs", stars = c(0.05), caption = "Conjoint Experiment Regression Models, Republican Respondents and Republican Judges", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors and update significance of effects
err.repsrep.conserv <- as.data.frame(reps.rep.ideo2$coefficients[, 2])
names(err.repsrep.conserv) <- c("Error")
err.repsrep.conserv$err2 <- round(err.repsrep.conserv$Error, 2)
err.repsrep.conserv

err.repsrep.sel <- as.data.frame(reps.rep.sel2$coefficients[, 2])
names(err.repsrep.sel) <- c("Error")
err.repsrep.sel$err2 <- round(err.repsrep.sel$Error, 2)
err.repsrep.sel

err.repsrep.pol <- as.data.frame(reps.rep.pol2$coefficients[, 2])
names(err.repsrep.pol) <- c("Error")
err.repsrep.pol$err2 <- round(err.repsrep.pol$Error, 2)
err.repsrep.pol

err.repsrep.ussc <- as.data.frame(reps.rep.ussc2$coefficients[, 2])
names(err.repsrep.ussc) <- c("Error")
err.repsrep.ussc$err2 <- round(err.repsrep.ussc$Error, 2)
err.repsrep.ussc



######################################################################
## Regression Models for Appendix Tables 7: Identity Congruence with Judge and Most Trusted to Hear Case
######################################################################


model.dem.in <- lm(selected ~ RaceIN + GenderIN + SexualityIN + PartyIN + agecat + lawschool + occupation
                  + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                  + factor(vignette_id),
                  data = dems)

# use new summary function
summary(model.dem.in)
model.dem.in2 <- summary(model.dem.in, cluster = c("id"))
model.dem.in2


model.rep.in <- lm(selected ~ RaceIN + GenderIN + SexualityIN + PartyIN + agecat + lawschool + occupation
                  + Rwoman + RLGB + Rincome + Rrelig + Rlibcon + Rrace + Redu + Rage + Rknow    
                  + factor(vignette_id),
                  data = reps)

# use new summary function
summary(model.rep.in)
model.rep.in2 <- summary(model.rep.in, cluster = c("id"))
model.rep.in2



texreg(list(model.dem.in, model.rep.in),
       custom.coef.names = c( 
         "(Intercept)",
         "Race ingroup",
         "Gender ingroup",
         "Sexuality ingroup",
         "Party ingroup",
         "50s",
         "60s",
         "70+",
         "Top 10, Private",
         "11-25, Private",
         "11-25, Public",
         "26-50, Private",
         "26-50, Public",
         "51-100, Private",
         "51-100, Public",
         "101-200, Private",
         "101-200, Public",
         "Civil rights",
         "Corporate",
         "Defense",
         "Prosecutor",
         "Public defend.",
         "RWoman",
         "LGB",
         "Income",
         "Religiosity",
         "Ideology",
         "RAsian",
         "RBlack",
         "Latino",
         "Multiracial",
         "RNative",
         "Other race",
         "Education",
         "Age",
         "Court knowledge",
         "Pairing 2",
         "Pairing 3",
         "Pairing 4",
         "Pairing 5"
         
       ),
       custom.model.names = c("Democrats", "Republicans"),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:sharedID", stars = c(0.05), caption = "Judge trusted to hear case, using identity
congruence with judge", float.pos = "hb", single.row = TRUE)

##get robust clustered standard errors
err.dem.ingroup <- as.data.frame(model.dem.in2$coefficients[,2])
names(err.dem.ingroup) <- c("Error")
err.dem.ingroup$err2 <- round(err.dem.ingroup$Error, 2)
err.dem.ingroup

err.rep.ingroup <- as.data.frame(model.rep.in2$coefficients[,2])
names(err.rep.ingroup) <- c("Error")
err.rep.ingroup$err2 <- round(err.rep.ingroup$Error, 2)
err.rep.ingroup

